package cn.nexd.map.coreLocator;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;

import cn.nexd.map.location.utils.Log;

/**
 * Created by admin on 15/8/27.
 */
public class coreAlgorithm {
    private coreLocationEngine cl = new coreLocationEngine();
    private coreWifiExtractor cw = new coreWifiExtractor();
    private coreFloorDetection cfd = new coreFloorDetection();

    //    public void initSet(String optpath, String pospath, String wifilistpath) throws IOException {
    //        coreFileLoader cf = new coreFileLoader();
    //        cf.loadOptParams(optpath);
    //        cf.loadWifiList(wifilistpath);
    //        cf.loadWp(pospath);
    //        _initSet(cf.opt_params, cf.wp, cf.wifi_list);
    //        cf = null;
    //        System.gc();
    //    }

    public void initSet(InputStream optpath, InputStream pospath, InputStream wifilistpath) throws IOException {
//        Log.d("Debug_round7", "Init Setup Start!");
        coreFileLoader cf = new coreFileLoader();
        cf.loadOptParams(optpath);
        cf.loadWifiList(wifilistpath);
        cf.loadWp(pospath);
        _initSet(cf.opt_params, cf.wp, cf.wifi_list);
//        Log.d("Debug_round7", "Init Setup Done!" + cf.wifi_list.toString());
        cf = null;
        System.gc();

    }

    public void floorSet(InputStream mean, InputStream var, InputStream weight, InputStream wifilist) throws IOException {
        cfd.loadConfiguration(mean, var, weight, wifilist);
    }

    private void _initSet(Double[][] opt, Double[][] pos, String[] wifilist) {
        this.cl.initMarkov(opt, pos);
        this.cw.setWifiList(wifilist);
    }

    public Double[] locate(String wifi) throws JSONException {
        Double[] res = cl.locate(cw.extractWifi(wifi));
        return res;
    }

    /*
    Test For Floor Detection Only
     */
    private Double[] locate_floor(String wifi) {
        Double[] res = new Double[2];
        res[0] = Math.random() * 200;
        res[1] = Math.random() * 200;
        return res;
    }

    public String locateOutput(String wifi) throws JSONException {
        Log.d("Debug_round1", "Location Service Started");
        //        Log.d("WifiInput", wifi);
        //        return _outputFormat(locate_floor(wifi), floorDetection(wifi));
        return _outputFormat(locate(wifi), floorDetection(wifi));
    }

    private String floorDetection(String wifi) throws JSONException {
        String floor_id = cfd.floorDetection(wifi, cw);
        return floor_id;
    }

    private String _outputFormat(Double[] res, String floorID) throws JSONException {

        JSONObject obj = new JSONObject();
        //        if (res == null) {
        //            obj.put("floor_id", BitmapUtils.floorId);
        //            obj.put("x", BitmapUtils.x);
        //            obj.put("y", BitmapUtils.y);
        //            return obj.toString();
        //        }
        obj.put("floor_id", floorID);
        //        BitmapUtils.floorId = floorID;
        obj.put("x", Double.toString(res[0]));
        //        BitmapUtils.x = Double.toString(res[0]);
        obj.put("y", Double.toString(res[1]));
        //        BitmapUtils.y = Double.toString(res[1]);
        return obj.toString();
    }
}
